﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BranchOfSansar.Sansar.Register;

namespace BranchOfSansar.Register.Products
{
    public partial class frmRegItem : Form
    {

        private Item CurrItem = null;
        private bool IsNew = false;
        
        
        public frmRegItem()
        {
            InitializeComponent();
            IsNew = true;
            CurrItem = new Item();

            using (BranchOfSansar.Sansar.Enquiry.EnquiryClient reg = new BranchOfSansar.Sansar.Enquiry.EnquiryClient(Globals.Token))
            {
                cmbCategory.Properties.Items.AddRange(reg.ItemCategories().Select(s => new BranchOfSansar.Sansar.Enquiry.ComboItem() { Display = s.Name, IntValue = s.ItemCate }).ToArray());
                cmbCompany.Properties.Items.AddRange(reg.CompanyList().Select(s => new BranchOfSansar.Sansar.Enquiry.ComboItem() { Display = s.Name, StrValue = s.CompanyNo }).ToArray());
                cmbCategory.SelectedIndex = 0;
                cmbCompany.SelectedIndex = 0;
                cmbStatus.SelectedIndex = 1;
            }
        }

        public frmRegItem(Item itm):this()
        {
            IsNew = false;
            CurrItem = itm;
            txtCode.Text = itm.ItemCode.ToString();
            txtName.Text = itm.Name;
            txtPrice.Text = itm.DefaultPrice.ToString("#,##0");
            txtSpeed.Text = itm.Speed.ToString("#,##0");
            Globals.setComboSelectedItem(cmbCategory, itm.ItemCate);
            Globals.setComboSelectedItem(cmbCompany, itm.CompanyNo);
            cmbStatus.SelectedIndex = itm.Status ? 1 : 0;

        }



        private void btnSave_Click(object sender, EventArgs e)
        {
            if(!Globals.DoValidation(pnlMain))
                return ;

            using (RegisterClient reg = new RegisterClient(Globals.Token))
            {
                CurrItem = new Item()
                {
                    CompanyNo = ((BranchOfSansar.Sansar.Enquiry.ComboItem)cmbCompany.SelectedItem).StrValue,
                    DefaultPrice = txtPrice.Value,
                    ItemCate = ((BranchOfSansar.Sansar.Enquiry.ComboItem)cmbCategory.SelectedItem).IntValue,
                    ItemCode = int.Parse(txtCode.Text),
                    Name = txtName.Text,
                    Speed = txtSpeed.Value,
                    Status = cmbStatus.SelectedIndex == 1
                };
                Result res;
                if (IsNew)
                    res=reg.AddItem(CurrItem);
                else
                    res=reg.EditItem(CurrItem);

                if (res.Code == 0)
                    DialogResult = System.Windows.Forms.DialogResult.OK;
                else
                {
                    Globals.Err(res.Message);
                }
            }

        }

        private void frmRegItem_Load(object sender, EventArgs e)
        {

        }
    }
}
